package cn.iocoder.yudao.module.exam.controller.admin.studentanswer;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.exam.controller.admin.studentanswer.vo.*;
import cn.iocoder.yudao.module.exam.dal.dataobject.studentanswer.StudentAnswerDO;
import cn.iocoder.yudao.module.exam.service.studentanswer.StudentAnswerService;

@Tag(name = "管理后台 - 考题管理")
@RestController
@RequestMapping("/exam/student-answer")
@Validated
public class StudentAnswerController {

    @Resource
    private StudentAnswerService studentAnswerService;

    @PostMapping("/create")
    @Operation(summary = "创建考题管理")
    @PreAuthorize("@ss.hasPermission('exam:student-answer:create')")
    public CommonResult<Long> createStudentAnswer(@Valid @RequestBody StudentAnswerSaveReqVO createReqVO) {
        return success(studentAnswerService.createStudentAnswer(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新考题管理")
    @PreAuthorize("@ss.hasPermission('exam:student-answer:update')")
    public CommonResult<Boolean> updateStudentAnswer(@Valid @RequestBody StudentAnswerSaveReqVO updateReqVO) {
        studentAnswerService.updateStudentAnswer(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除考题管理")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('exam:student-answer:delete')")
    public CommonResult<Boolean> deleteStudentAnswer(@RequestParam("id") Long id) {
        studentAnswerService.deleteStudentAnswer(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除考题管理")
                @PreAuthorize("@ss.hasPermission('exam:student-answer:delete')")
    public CommonResult<Boolean> deleteStudentAnswerList(@RequestParam("ids") List<Long> ids) {
        studentAnswerService.deleteStudentAnswerListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得考题管理")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('exam:student-answer:query')")
    public CommonResult<StudentAnswerRespVO> getStudentAnswer(@RequestParam("id") Long id) {
        StudentAnswerDO studentAnswer = studentAnswerService.getStudentAnswer(id);
        return success(BeanUtils.toBean(studentAnswer, StudentAnswerRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得考题管理分页")
    @PreAuthorize("@ss.hasPermission('exam:student-answer:query')")
    public CommonResult<PageResult<StudentAnswerRespVO>> getStudentAnswerPage(@Valid StudentAnswerPageReqVO pageReqVO) {
        PageResult<StudentAnswerDO> pageResult = studentAnswerService.getStudentAnswerPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, StudentAnswerRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出考题管理 Excel")
    @PreAuthorize("@ss.hasPermission('exam:student-answer:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportStudentAnswerExcel(@Valid StudentAnswerPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<StudentAnswerDO> list = studentAnswerService.getStudentAnswerPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "考题管理.xls", "数据", StudentAnswerRespVO.class,
                        BeanUtils.toBean(list, StudentAnswerRespVO.class));
    }

}